package com.it.shiro.shiro;



import com.it.base.service.impl.BaseService;
import com.it.shiro.sys.entity.Menu;
import com.it.shiro.sys.entity.User;
import com.it.shiro.sys.mapper.MenuMapper;
import com.it.shiro.sys.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: learning
 * @description: 认证授权接口
 * @author: Lefnmg
 * @create: 2018-11-11 16:14
 **/
@Service
public class ShiroService extends BaseService<User,UserMapper> {
    @Autowired
    MenuMapper menuMapper;
    public Set<String> getUserPermissions(Long userId) {
        List<String> permsList;

        //系统管理员，拥有最高权限
        if(userId == 1L){
            List<Menu> menuList = menuMapper.findList(null);
            permsList = menuList.stream()
                    .map(menu -> menu.getPerms())
                    .collect(Collectors.toList());
        }else{
            permsList = menuMapper.findPermsByUser(userId);
        }
        //用户权限列表
        Set<String> permsSet = new HashSet<>();
        for(String perms : permsList){
            if(StringUtils.isBlank(perms)){
                continue;
            }
            permsSet.addAll(Arrays.asList(perms.trim().split(",")));
        }
        return permsSet;
    }
}
